package cn.itsource.controller;

import cn.itsource.domain.Comment;
import cn.itsource.service.CommentService;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 接口类：评论Comment
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加和修改
     * @param comment
     * @return
     */
    @PutMapping
    public AjaxResult addorUpdate(@RequestBody Comment comment){
        try {
            if(comment.get_id()==null){//添加操作
                commentService.add(comment);
            }else{
                commentService.update(comment);
            }
            //如果成功
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙,请稍后重试");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")String id){
        try {
            commentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙,请稍后重试");
        }
    }

    /**
     * 根据文章id获取评论
     * @param articleId
     * @return
     */
    @GetMapping("/{articleId}")
    public List<Comment> findComment(@PathVariable("articleId")Long articleId){
        return commentService.findByArticleId(articleId);
    }

}
